package com.example.coffeeseller_2.utils;

import android.content.Context;
import android.speech.tts.TextToSpeech;
import android.util.Log;
import android.widget.Toast;

import java.util.Locale;

/**
 * Created by Administrator on 2018/3/23.
 */

public class SingleTTs implements TextToSpeech.OnInitListener {
    private static SingleTTs mInstance;
    private TextToSpeech mTextToSpeech;
    private Context mContext;
    String TAG = "SingleTTs";
//    private OnHumanInductionListener mOnHumanInductionListener;
   /* public void setTextToSpeech(Activity context) {

    }*/

 /*   public SingleTTs(Context mContext) {

    }*/

    public static SingleTTs getSingleTTs() {
        if (mInstance == null) {
            mInstance = new SingleTTs();

        }
        return mInstance;
    }

    /*public void setOnHumanInductionListener(OnHumanInductionListener mOnHumanInductionListener) {
        if (mOnHumanInductionListener != null) {
            this.mOnHumanInductionListener = mOnHumanInductionListener;
        }
    }*/

    public void init(Context context) {

//        CPUMsger.getCPUMsger().setCheckMachineStateListener(this);
        this.mContext = context;
        long curTime = System.currentTimeMillis();
        mTextToSpeech = new TextToSpeech(mContext, this);
        mTextToSpeech.setPitch(0.0f);
        Log.d(TAG, "---" + (System.currentTimeMillis() - curTime));

    }

    public void Speak(String Contest) {
        if (mTextToSpeech != null && !mTextToSpeech.isSpeaking()) {
            mTextToSpeech.speak(Contest,
                    TextToSpeech.QUEUE_FLUSH, null);

        } else {
            MyLog.d(TAG, "speak erro");
        }
    }

    public void SpeakFirst(String Contest) {
        if (mTextToSpeech != null) {
            if (mTextToSpeech.isSpeaking()) {
                mTextToSpeech.stop();//如果当前正在播报语音则停止当前播报
            }
            mTextToSpeech.speak(Contest,
                    TextToSpeech.QUEUE_FLUSH, null);
        }
    }

    @Override
    public void onInit(int status) {
        if (status == TextToSpeech.SUCCESS) {

            int result = mTextToSpeech.setLanguage(Locale.US);
            int reSulit2 = mTextToSpeech.setLanguage(Locale.CHINA);
            if (result == TextToSpeech.LANG_MISSING_DATA
                    || result == TextToSpeech.LANG_NOT_SUPPORTED) {
                Toast.makeText(mContext, "language is not supported", Toast.LENGTH_SHORT).show();
            } else if ((reSulit2 != TextToSpeech.LANG_AVAILABLE) && (reSulit2 != TextToSpeech.LANG_COUNTRY_AVAILABLE)) {
                Toast.makeText(mContext, "language is not supported", Toast.LENGTH_SHORT).show();
            }
        } else {
            Toast.makeText(mContext,"language is not supported", Toast.LENGTH_SHORT).show();
        }
    }

    public void stopTTs() {
        if (mInstance != null && mTextToSpeech != null) {
            mTextToSpeech.stop();
            mTextToSpeech.shutdown();
        } else {
            Log.e(TAG, "tts or single is null");
        }
    }

    private int LastState = 0;
    private int CurrentState = 0;

/*    @Override
    public void getMachineState(MachineState machineState) {
        if (machineState == null) {
            return;
        }
        int[] sensorsState = machineState.getStateOfSensors();
        if (sensorsState == null || sensorsState.length < 29) {
            return;
        }
        CurrentState = sensorsState[28];
        if (LastState == 0 && CurrentState == 1) {
            //
            if (mOnHumanInductionListener != null) {
                if (CurPageRecorder.curPage == PageEnum.ChooseCupNumPage ||
                        CurPageRecorder.curPage == PageEnum.WaitJuicePage
                        ) {
                    mOnHumanInductionListener.HumanComing();
                }

            }

        } else if (LastState == 1 && CurrentState == 0) {
            if (mOnHumanInductionListener != null) {
                if (CurPageRecorder.curPage == PageEnum.ChooseCupNumPage ||
                        CurPageRecorder.curPage == PageEnum.WaitJuicePage
                        ) {
                    mOnHumanInductionListener.HumanLeave();
                }

            }

        }
        LastState = CurrentState;
    }*/
}
